<?php

class Controller_Jqeffects extends Controller_Abstract
{
    function actionIndex()
    {
    	$page = $this->_context->get('page', 1);
    	$jqeffectclass = $this->_context->get('jqeffectclass', array());
    	$jqEffectsModel = new JqEffects();
		$jqEffectsModel->pageLimit = 5;
        $result = $jqEffectsModel->getJqeffects($jqeffectclass, $page);
        
    	$this->_view['jqEffects'] =  $result['data'];
    	$pagination = new Pagination($result['totalNum'], $this->_context->requestUri(), $page, $jqEffectsModel->pageLimit);
    	$this->_view['pagination'] = $pagination->render();
	    $this->_view['classification'] = $jqEffectsModel->getAllClassification();   	dump($this->_view['classification']);
	    $this->_view['jqeffectclass'] = empty($jqeffectclass)?  $this->_view['classification'] : $jqeffectclass;
    	

    }
    
    function actionDemo()
    {
    	if($this->_context->isGET()){
    		$jqeId = $this->_context->get('jqeId', false);
    		if($jqeId){
    			$jqeId = Util::UrlParamDecode($jqeId);
    			$jqEffectsModel = new JqEffects();
    			$jqeffect = $jqEffectsModel->getJqeffectById($jqeId);
    			if(!$jqeffect){
    				return $this->_redirect(url('errorpages/jqeffects', array('error' => 'iderror')));
    			}
    				
    			$this->_view['jqeffect'] = $jqeffect;
    		}else{
    			exit();
    		}
    	}
    }
    function actionDemoiframe()
    {
    	if($this->_context->isGET()){
    		$jqeId = $this->_context->get('jqeId', false);
    		if($jqeId){
    			$jqeId = Util::UrlParamDecode($jqeId);
    			$jqEffectsModel = new JqEffects();
    			$jqeffect = $jqEffectsModel->getJqeffectById($jqeId);
    			if(!$jqeffect){
    				return $this->_redirect(url('errorpages/jqeffects', array('error' => 'iderror')));
    			}
    			
    			$this->_view['contents'] = file_get_contents($this->_jqEffectsRoot . $jqeffect['name'] . DIRECTORY_SEPARATOR . $jqeffect['demo_file']);
    		}else{
    			exit();
    		}
    	}
    }
    
    function actionImg()
    {
    	if($this->_context->isGET()){
    		$jqeId = $this->_context->get('jqeId', false);
    		if($jqeId){
    			$jqeId = Util::UrlParamDecode($jqeId);
    			$jqEffectsModel = new JqEffects();
    			$jqeffect = $jqEffectsModel->getJqeffectById($jqeId);
    			if($jqeffect){
    				header('Content-Type: image/gif');
	    			echo file_get_contents($this->_jqEffectsRoot . $jqeffect['name'] . DIRECTORY_SEPARATOR . $jqeffect['image_file']);
    			}
    			
    			exit();
    		}else{
    			exit();
    		}
    	}
    }
    
    function actionDownload()
    {
    	if($this->_context->isGET()){
    		$jqeId = $this->_context->get('jqeId', false);
    		if($jqeId){
    			$jqeId = Util::UrlParamDecode($jqeId);
    			$jqEffectsModel = new JqEffects();
    			$jqeffect = $jqEffectsModel->getJqeffectById($jqeId);
    			if(!$jqeffect){
    				return $this->_redirect(url('errorpages/jqeffects', array('error' => 'iderror')));
	    		}
    			
    			$file_name = $jqeffect['pack_file'];
				$file_dir = $this->_jqEffectsRoot . $jqeffect['name'] . DIRECTORY_SEPARATOR . $file_name;
				if (!file_exists($file_dir)) {
					exit;
				} else {
					$file = fopen($file_dir, "r");
					// file tag
					Header("Content-type: application/octet-stream");
					Header("Accept-Ranges: bytes");
					Header("Accept-Length: ".filesize($file_dir));
					Header("Content-Disposition: attachment; filename=" . $file_name);
					
					echo fread($file,filesize($file_dir));
					fclose($file);
					exit;
				}
    		}else{
    			exit();
    		}
    	}
    }
    
    function actionSynchronizeDbToRealPathAbs()					//move this to backend later
    {
    	die();
    	
    	$jqEffectsModel = new JqEffects();
    	$jqEffectsModel->synchronizeDbToRealPathAbs($this->_jqEffectsRoot);
    }
    
    function actionSynchronizeAdd()								//move this to backend later
    {
    	die();
    	
    	$jqEffectsModel = new JqEffects();
    	$jqEffectsModel->synchronizeDbToRealPathAdd($this->_jqEffectsRoot);
    }
    
}

